package com.imageim.yimirror.common.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.imageim.yimirror.R;
import com.nineoldandroids.animation.ObjectAnimator;

/**
 * Created by Bruce.Zhou on 2016/5/2.
 */
public class LoadingDialog extends PromptDialog {

    private TextView tvMsg;
    private String msg;

    public LoadingDialog(Context context) {
        super(context);
    }

    public LoadingDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected LoadingDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        bindView(R.layout.dialog_loading, new BindViewListener() {
            @Override
            public void onBindView(View contentView) {
                View loading = contentView.findViewById(R.id.iv_loading);
                ObjectAnimator anim = ObjectAnimator.ofFloat(loading, "rotation", 0, 360).setDuration(1000);
                anim.setRepeatCount(ObjectAnimator.INFINITE);
                anim.start();
                tvMsg = contentView.findViewById(R.id.tip);
                if (!TextUtils.isEmpty(msg)) {
                    tvMsg.setText(msg);
                }else {
                    tvMsg.setVisibility(View.GONE);
                }
            }
        });
        super.onCreate(savedInstanceState);
    }

    public void setMsg(String msg) {
        this.msg = msg;
        if (tvMsg != null) {
            tvMsg.setText(msg);
        }
    }
}
